/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Reflection;
using Core.Model.Ingredients.Presentable.Visibles;
using Rectangle=System.Drawing.Rectangle;

namespace WinForms.Renderer.GDI.Drawers
{
    public class RectangleDrawer : Drawer
    {
        public override Type GetVisible
        {
            get
            {
                //Assembly assembly = Assembly.Load( "OpenMHEG.Core" );
                //return assembly.GetType( "Core.Model.Rectangle" , true );
                return typeof(Core.Model.Ingredients.Presentable.Visibles.Rectangle);
            }
        }

        public override void Draw ( Visible v, Graphics g )
        {
            var rectangle = (Core.Model.Ingredients.Presentable.Visibles.Rectangle)v;

            int lineWidth = rectangle.LineWidth;
            
            if ( lineWidth == 0 || rectangle.RefLineColour.Value.Equals(rectangle.RefFillColour.Value) )
            {
                Rectangle inner = new Rectangle( rectangle.BoxPosition, rectangle.BoxSize );
                using ( Brush brush = new SolidBrush( rectangle.RefFillColour.Value ) )
                {
                    g.FillRectangle( brush, inner );
                }
            }

            else
            {

                Rectangle inner = new Rectangle( rectangle.BoxPosition.X + lineWidth,
                                                 rectangle.BoxPosition.Y + lineWidth,
                                                 rectangle.BoxSize.Width - ( 2 * lineWidth ),
                                                 rectangle.BoxSize.Height - ( 2 * lineWidth ) );

                Rectangle outer = new Rectangle( rectangle.BoxPosition, rectangle.BoxSize );


                using ( Pen pen = new Pen( rectangle.RefLineColour.Value, rectangle.LineWidth ) )
                {

                    switch ( rectangle.LineStyle )
                    {
                        case LineArt.LineArtLineStyle.Solid:
                            pen.DashStyle  = System.Drawing.Drawing2D.DashStyle.Solid;
                            break;

                        case LineArt.LineArtLineStyle.Dotted:
                            pen.DashStyle  = System.Drawing.Drawing2D.DashStyle.Dot;
                            break;

                        case LineArt.LineArtLineStyle.Dashed:
                            pen.DashStyle  = System.Drawing.Drawing2D.DashStyle.Dash;
                            break;

                        default:
                            pen.DashStyle  = System.Drawing.Drawing2D.DashStyle.Solid;
                            break;
                    }


                    using ( Brush brush = new SolidBrush( rectangle.RefFillColour.Value ) )
                    {

                        g.DrawRectangle( pen, outer );
                        g.FillRectangle( brush, inner );
                    }
                }
            }
        }
    }
}
